using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BestLifePOS_Core.Data.Core;
using BestLifePOS_Core.DomainObjects;
using System.Data.SqlClient;
using System.Data;
using BestLifePOS_Core.Exceptions.DataBase;

namespace BestLifePOS_Core.Data.Commands
{
    class BestLifeuserByunamewithcheckpassword : BestLifeDataAction<List<User>>
    {
        string Lvusername = "";
        string Lvpassword = "";

        public BestLifeuserByunamewithcheckpassword(string pusername,string ppassword)
        {
            Lvusername=pusername;
            Lvpassword = ppassword;
        }

        public override List<User> Body(SqlConnection PConn)
        {
            try
            {
                Boolean lvdatast = false;
                List<User> LvListreturn = new List<User>();
                List<User> LvList = new List<User>();
                SqlCommand LvCommand = new SqlCommand();
                LvCommand.Connection = PConn;
                LvCommand.CommandType = CommandType.StoredProcedure;
                LvCommand.CommandText = "dbo.GetUserByUsername";
                LvCommand.Parameters.Add(new SqlParameter("@User_Name",Lvusername));//when passing parameters to the SP

                SqlDataReader LvReader = LvCommand.ExecuteReader();

                
                    while (LvReader.Read())
                    {
                        User Lvuser = new User();
                        Lvuser.User_Id = LvReader.GetValue(0).ToString();
                        Lvuser.Use_Type = LvReader.GetValue(1).ToString();
                        Lvuser.Use_Name = LvReader.GetValue(2).ToString();
                        Lvuser.Use_Id = LvReader.GetValue(3).ToString();
                        Lvuser.Last_Modify = DateTime.Parse(LvReader.GetValue(4).ToString());
                        Lvuser.Pas_Word = LvReader.GetValue(5).ToString();
                        Lvuser.User_Access = LvReader.GetValue(6).ToString();
                        Lvuser.User_Modify = LvReader.GetValue(7).ToString();
                        Lvuser.Date_Create = DateTime.Parse(LvReader.GetValue(8).ToString());
                        Lvuser.Branch_Id = LvReader.GetValue(9).ToString();

                        LvList.Add(Lvuser);
                        lvdatast = true;
                    }

                    #region Check PassWord
                    if (lvdatast)
                    {
                        if (LvList[0].Pas_Word.Equals(Lvpassword))
                        {
                            LvListreturn = LvList;
                        }
                        else
                        {
                            LvListreturn = null;
                        }
                    #endregion
                    }
                    else { LvListreturn = null; }
                return LvListreturn;

            }
            catch (BestlifeDatabaseException e)
            {
                throw new BestlifeDatabaseException(e);
            }
        }

    }
}
